import requst from '../../../util/Http.js'
Page({
  data:{
    goods: {
      id: 1,
      image: '/image/goods1.png',
      title: '新鲜梨花带雨',
      price: 0.01,
      stock: '有货',
      detail: '这里是梨花带雨详情。',
      parameter: '125g/个',
      service: '不支持退货'
    },
    num: 1,
    totalNum: 0,
    hasCarts: false,
    curIndex: 0,
    show: false,
    scaleCart: false
  },
    // 页面初始化 options为页面跳转所带来的参数
    onLoad:function(options){
      let than=this;
      console.log(options)
        //  获取主题详情
        requst('Index/getShopDetli',{id:options.id},'post').then(res=>{
          than.setData({
            goods:res.data
          })
          console.log(res.data)
        })
    },

  setCart(){
    let token=wx.getStorageSync('token')
    if(token.length==0){
      wx.navigateTo({
        url: '/page/component/login/login',
      })
      return
    }

    let {totalNum,goods}=this.data;
    goods.totalNum=totalNum;
    goods.selected=true;
    let list=wx.getStorageSync('cartList')
    if(list.length>0){
      for(let i=0;i<list.length;i++){
        if(list[i].id===goods.id){
          list[i].totalNum+=goods.totalNum;
          wx.setStorageSync('cartList',list);
          break;
        }else{
          list.push(goods)
          wx.setStorageSync('cartList',list);
          break;
        }
      }
    }else{
      wx.setStorageSync('cartList',[goods])
    }
    
    wx.switchTab({
      url: '/page/component/cart/cart',
    })
  }, 

  addCount(e) {
    let num = this.data.num;
    let index=e.currentTarget.dataset.index;
    if(index!=undefined){
      num+=parseInt(index)
      if(num==0){
        num=1
        return
      }
    }else{
      num++
    }
    this.setData({
      num : num
    })
  },

  addToCart() {
    console.log(2)
    const self = this;
    const num = this.data.num;
    let total = this.data.totalNum;
    // {id:2,title:'素米 500g',image:'/image/s6.png',num:1,price:0.03,selected:true}

    self.setData({
      show: true
    })
    setTimeout( function() {
      self.setData({
        show: false,
        scaleCart : true
      })
      setTimeout( function() {
        self.setData({
          scaleCart: false,
          hasCarts : true,
          totalNum: num + total
        })
      }, 200)
    }, 300)

  },

  bindTap(e) {
    const index = parseInt(e.currentTarget.dataset.index);
    this.setData({
      curIndex: index
    })
  }
 
})